﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DevComponents.DotNetBar.SuperGrid;
using DevComponents.DotNetBar.SuperGrid.Style;
using Platform.Model;
using Platform.Model.Repositories;
using PlatformManagement.Properties;
using BLToolkit.Data;
using Platform.Utilities;

namespace PlatformManagement
{
    public partial class EmployeeForm : DevComponents.DotNetBar.Metro.MetroForm
    {
        bool _currentDerty = false;
        public EmployeeForm()
        {
            InitializeComponent();
            dgvEmployee.AutoGenerateColumns = false;
        }
        protected override void OnActivated(EventArgs e)
        {
            base.OnActivated(e);
            this.WindowState = FormWindowState.Maximized;
        }

        protected override void OnShown(EventArgs e)
        {
            var bsEmployee = new BindingSource { DataSource = Employee.Objects.ToList() };
            dgvEmployee.DataSource = bsEmployee;
            base.OnShown(e);
        }

        private void BtnDelete_Click(object sender, EventArgs e)
        {
            // Lấy những dòng được đánh dấu check
            var selectedRows = dgvEmployee.Rows.Cast<DataGridViewRow>()
                    .Where(x => x.Cells[0].Value != null && x.Cells[1].Value != null && (bool)x.Cells[0].Value)
                    .ToList();
            if (selectedRows.Count == 0)
            {
                MessageBox.Show("Please select employee by checking on checkbox.", "Message",
                                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            var message = "All selected employee and related data will be deleted forever. Are you sure?";

            // Xuất thông báo cho việc xóa
            if (MessageBox.Show(message, "Warning !!!", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                // Duyệt qua từng dòng được chọn
                for (int i = selectedRows.Count - 1; i >= 0; i--)
                {
                    int id = Convert.ToInt32(selectedRows[i].Cells[1].Value.ToString());
                    Employee.Objects.DeleteByKey(id);

                }
                dgvEmployee.DataSource = new BindingSource { DataSource = Employee.Objects.ToList() };
            }
        }
        private void dgvEmployee_RowValidated(object sender, DataGridViewCellEventArgs e)
        {
            if (_currentDerty)
            {
                try
                {
                    var cur = dgvEmployee.Rows[e.RowIndex];
                    var ep = Employee.Objects.ToList();
                    if (cur.Cells[1].Value.ToString() == "0")
                    {
                        bool kt = false;
                        for (int i = 0; i < ep.Count; i++)
                        {
                            if (ep[i].Username == cur.Cells[2].Value.ToString())
                                kt = true;
                        }
                        if (!kt)
                        {
                            var emp = new Employee
                            {
                                Username = cur.Cells[2].Value.ToString(),
                                Password = Util.Shared.GetMD5(cur.Cells[3].Value.ToString()),
                                FirstName = cur.Cells[4].Value.ToString(),
                                LastName = cur.Cells[5].Value.ToString(),
                                Address = cur.Cells[6].Value.ToString(),
                                Tel = cur.Cells[7].Value.ToString(),
                                Status = (bool)cur.Cells[8].Value,
                                LastLogin = null,
                                LastUpdated = null,
                                RoleId = int.Parse(cur.Cells[11].Value.ToString())

                            };

                            Employee.Objects.Insert(emp);
                        }
                        else
                        {
                            MessageBox.Show("Usename value must be unique");
                            return;
                        }
                        dgvEmployee.DataSource = new BindingSource { DataSource = Employee.Objects.ToList() };
                    }
                    else
                    {
                        var emp = new Employee
                        {
                            ID = int.Parse(cur.Cells[1].Value.ToString()),
                            Username = cur.Cells[2].Value.ToString(),
                            Password = Util.Shared.GetMD5(cur.Cells[3].Value.ToString()),
                            FirstName = cur.Cells[4].Value.ToString(),
                            LastName = cur.Cells[5].Value.ToString(),
                            Address = cur.Cells[6].Value.ToString(),
                            Tel = cur.Cells[7].Value.ToString(),
                            Status = (bool)cur.Cells[8].Value,
                            LastLogin = null,
                            LastUpdated = DateTime.Now,
                            RoleId = int.Parse(cur.Cells[11].Value.ToString())

                        };
                        Employee.Objects.Update(emp);
                        dgvEmployee.DataSource = new BindingSource { DataSource = Employee.Objects.ToList() };
                    }

                }
                catch (Exception ex)
                {

                    MessageBox.Show("Error: " + ex.Message);
                }
            }
            _currentDerty = false;
        }

        private void dgvEmployee_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
        {
            try
            {
                
                if (dgvEmployee.IsCurrentRowDirty && e.ColumnIndex > 0)
                {
                    var currentRow = dgvEmployee.Rows[e.RowIndex];
                    if (currentRow.Cells[2].Value == null || currentRow.Cells[3].Value == null ||
                             string.IsNullOrEmpty(currentRow.Cells[2].Value.ToString()) ||
                             string.IsNullOrEmpty(currentRow.Cells[3].Value.ToString()) ||
                             currentRow.Cells[4].Value == null || currentRow.Cells[5].Value == null ||
                             currentRow.Cells[6].Value == null || currentRow.Cells[7].Value == null ||
                             currentRow.Cells[8].Value == null)
                    {
                        MessageBox.Show("You have to enter value for all cells", "Error",
                                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                        e.Cancel = true;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
            }
            _currentDerty = dgvEmployee.IsCurrentRowDirty;
        }

        private void BtnResetPass_Click(object sender, EventArgs e)
        {
            // Lấy những dòng được đánh dấu check
            var selectedRows = dgvEmployee.Rows.Cast<DataGridViewRow>()
                    .Where(x => x.Cells[0].Value != null && x.Cells[1].Value != null && (bool)x.Cells[0].Value)
                    .ToList();
            if (selectedRows.Count == 0)
            {
                MessageBox.Show("Please select employee by checking on checkbox.", "Message",
                                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            var message = "All selected employee will be reset password. Are you sure?";

            // Xuất thông báo cho việc reset password
            if (MessageBox.Show(message, "Warning !!!", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                // Duyệt qua từng dòng được chọn
                for (int i = selectedRows.Count - 1; i >= 0; i--)
                {
                    Employee ep = selectedRows[i].DataBoundItem as Employee;
                    ep.Password = Util.Shared.GetMD5(ep.Username);
                    Employee.Objects.Update(ep);
                }
                dgvEmployee.DataSource = new BindingSource { DataSource = Employee.Objects.ToList() };
            }
        }

    }
}
